<template>
    <!-- use the modal component, pass in the prop -->
    <modal v-if="stateManager.createDialogVisible"
           @cancel="closeDialog"
           @ok="saveModel"
           :modal-title="modalTitle"
           model-size="normal">
        <!--
          you can use custom content here to overwrite
          default content
        -->
        <vue-form>
            <div class="row">
                <div class="col-xs-12">
                    <vue-form-item label="用户账户" :error-message="model.errors.name || errors.first('name')">
                        <input-text v-model:value="model.name"
                                    v-validate="'required'"
                                    label="用户账户"
                                    el-name="name"></input-text>
                    </vue-form-item>
                    <vue-form-item label="邮箱" :error-message="model.errors.email || errors.first('email')">
                        <input-text v-model:value="model.email"
                                    v-validate="'required|email'"
                                    label="邮箱"
                                    el-name="email"></input-text>
                    </vue-form-item>
                    <!--<vue-form-item label="密码" :error-message="model.errors.password || errors.first('password')">-->
                        <!--<input-password v-model:value="model.password"-->
                                    <!--v-validate="'required'"-->
                                    <!--label="密码"-->
                                    <!--el-name="password"></input-password>-->
                    <!--</vue-form-item>-->
                    <vue-form-item label="账户密码" :error-message="model.errors.password|| errors.first('password')">
                        <input-password v-model:value="model.password"
                                        v-validate="'required'"
                                        label="账户密码"
                                        el-name="password"></input-password>
                    </vue-form-item>

                </div>
            </div>
        </vue-form>


    </modal>


</template>

<script>
    export default {
        props: {
            stateManager: {
                type: Object
            }
        },
        data: function () {
            return {
                model: this.$modelDataSource({
                    url: 'api/users',
                    dataKey: 'model',
                    attributes: {
                        name:'',
                        email:'',
                        password:'',
                    }
                })
            };
        },
        methods: {
            closeDialog: function () {
                this.stateManager.createDialogVisible = false;
                this.model.reset();
            },
            saveModel: function () {
                let self = this;
                self.validate(function () {
                    self.model.save(function () {
                        self.model.reset();
                        self.closeDialog();
                        self.stateManager.refreshList = true;
                        toastr.success(self.model.name + '保存成功！');
                    }, function () {
                        if (self.model.errors.permissions_errors) {
                            toastr.error(self.model.errors.permissions_errors);
                        }
                    });
                });
            },

            validate: function (successCallback, failedCallback) {
                this.$validator.validateAll().then(() => {
                    // eslint-disable-next-line
                    console.log('验证通过');
                    typeof successCallback === 'function' && successCallback();
                }).catch(() => {
                    // eslint-disable-next-line
                    console.log('验证未通过');
                    typeof failedCallback === 'function' && failedCallback();
                });

            }
        },
        computed: {
            modalTitle: function () {
                return '添加工作人员';
            }
        }
    }
</script>
